﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using freemarker.template;
using System.Web;

namespace Freemarker.Net.MvcViewEngine.FreemarkerModel {
    public class UrlDirectiveModel : TemplateDirectiveModel {
        static UrlDirectiveModel instance = new UrlDirectiveModel();
        public static UrlDirectiveModel Instance {
            get {
                return instance;
            }
        }

        public void execute(freemarker.core.Environment e, java.util.Map m, TemplateModel[] tmarr, TemplateDirectiveBody tdb) {
            if (m.containsKey("path")) {
                var path = ((TemplateScalarModel)m.get("path")).getAsString();
                e.getOut().write(
                    (HttpContext.Current.Request.ApplicationPath +
                    path.Substring(1)).Replace("//", "/"));
            }
        }
    }
}
